package com.holic.db;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.holic.bean.Product;
import com.holic.util.JdbcUtil;

public class ProductDao {
	
	/**
	 * 批量新增
	 * @param products
	 * @throws SQLException
	 */
	public int[] insert(List<Product> products) throws SQLException{
		int len = products.size();
		Object[][] params = new Object[len][7];
		for(int i=0; i<len; i++){
			Product product = products.get(i);
			int index = 0;
			params[i][index++] = product.getId();
			params[i][index++] = product.getTitle();
			params[i][index++] = product.getThumbnail();
			params[i][index++] = product.getCategory();
			params[i][index++] = product.getImgs();
		}
		String sql = "INSERT INTO t_product(id, title, thumbnail, category, imgs) VALUES(?,?,?,?,?)";
		QueryRunner query = new QueryRunner(true);
		return query.batch(JdbcUtil.getConnection(), sql, params);
	}
	
	/**
	 * 根据id 查询
	 * @param profileId
	 * @return
	 * @throws SQLException
	 */
	public List<Product> query(long id) throws SQLException{
		String sql = "SELECT * FROM t_product WHERE id=?";
		QueryRunner query = new QueryRunner(true);
		List<Product> products = query.query(JdbcUtil.getConnection(), sql, new BeanListHandler<Product>(Product.class), id);
		return products;
	}
	
}
